package com.transcargo.users;

import com.transcargo.db.dbException;
import com.transcargo.db.db;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

/**
 * Date: 17.02.2011
 * Time: 14:05:00
 */
public class Transport {
    private int Id = -1;
    private int User_Id = -1;
    private String name = "";
    private String number = "";
    private int type;
    private int[] loading;
    private float height;
    private float width;
    private float length;
    private Boolean tir;
    private Boolean ekmt;
    private int adr;
    private int tonnage;
    private int volume;
    public Transport(int id, int user_id, db Db) throws dbException,TransportNotFoundException {
        Id = id;
        User_Id = user_id;
        ArrayList<String[]> list = Db.execute("SELECT name, type, loading, height, width, length, tir, ekmt, adr, tonnage, volume FROM transport WHERE id=" + Integer.toString(Id) + " and user_id=" + Integer.toString(User_Id));
        if (list != null) {
            Iterator<String[]> iter =list.iterator();
            if (iter != null && iter.hasNext()) {
                String[] ctr = iter.next();
                name = ctr[0];
                type = Integer.parseInt(ctr[1]);
                if (ctr[2] != null && ctr[2].length() > 0) {
                	StringTokenizer st = new StringTokenizer(ctr[2],",");
                	loading = new int[st.countTokens()];
                	int j = 0;
                	while (st.hasMoreTokens()) {
                		String s = st.nextToken();
                		loading[j] = Integer.parseInt(s);
                		j++;
                	}
                }
                if (ctr[3] != null) height = Float.parseFloat(ctr[3]); else height = 0;
                if (ctr[4] != null) width = Float.parseFloat(ctr[4]); else width = 0;
                if (ctr[5] != null) length = Float.parseFloat(ctr[5]); else length = 0;
                tir = ctr[6].equals("1");
                ekmt = ctr[7].equals("1");
                adr = Integer.parseInt(ctr[8]);
                tonnage = Integer.parseInt(ctr[9]);
                volume = Integer.parseInt(ctr[10]);
            } else throw new TransportNotFoundException(id);
        }
    }
    public int getId() {
        return Id;
    }
    public int getUserId() {
        return User_Id;
    }
    public String getName() {
        return name;
    }
    public int getType() {
        return type;
    }
    public String getSType(db dbase) throws dbException {
        ArrayList<String[]> list = dbase.execute("SELECT name FROM transport_type WHERE id=" + Integer.toString(type));
        if (list != null) {
            Iterator<String[]> iter =list.iterator();
            if (iter != null && iter.hasNext()) {
                String[] ctr = iter.next();
                return ctr[0];
            }
        }
       return "";
    }
    public int[] getLoading() {
        return loading;
    }
    public String[] getSLoading(db dbase) throws dbException {
        String[] s = new String[loading.length];
        for (int i = 0; i < loading.length; i++) {
            ArrayList<String[]> list = dbase.execute("SELECT name FROM transport_loading WHERE id=" + Integer.toString(loading[i]));
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter != null && iter.hasNext()) {
                    String[] ctr = iter.next();
                    s[i] = ctr[0];
                }
            }
        }
        return s;
    }

    public float getHeight() {
        return height;
    }
    public float getWidth() {
        return width;
    }
    public float getLength() {
        return length;
    }
    public Boolean getTIR() {
        return tir;
    }
    public Boolean getEKMT() {
        return ekmt;
    }
    public int getADR() {
        return adr;
    }
    public int getTonnage() {
        return tonnage;
    }
    public int getVolume() {
        return volume;
    }

}
